# 文件名: ck_data_preparation
# 当前用户: shiping.zheng
# 当前日期: 2024/9/26
# 当前时间: 14:04
# 项目名称: Flag
"""
模块描述:
    
"""
import requests
from datetime import datetime
import time
from ck_connect import ClickHouseConnector
import logging

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


class CK_Data_Preparation:
    def __init__(self):
        self.connector = ClickHouseConnector(host='10.51.2.30',port=8123,username=None,password=None,database=None)

    def data_batchpreparation(self, sql):
        result = self.connector.execute_queries(sql)
        if not result:
            logging.error("No result returned from the query execution.")
            return None
        else:
            # 检查是否有失败的操作
            for res in result:
                if isinstance(res, str) and "failed" in res:
                    logging.error(f"Query execution failed: {res}")
            return result

    def data_single_preparation(self, sql):
        result = self.connector.execute_query(sql)
        if not result:
            logging.error("No result returned from the query execution.")
            return None
        else:
            # 检查是否有失败的操作
            for res in result:
                if isinstance(res, str) and "failed" in res:
                    logging.error(f"Query execution failed: {res}")
            return result

    def data_select(self,sql):
        result = self.connector.execute_query(sql)
        # 提取单个值
        if result and len(result) > 0 and len(result[0]) > 0:
            if result == None:
                return 0
            else:
                return int(float(result[0][0]))  # 返回第一个行的第一个元素并且转化为int
        else:
            return 0  # 如果结果为空，返回 0


if __name__ == '__main__':
    # 创建 CK_Data_Preparation 的实例
    data_preparation = CK_Data_Preparation()

    # sql = ("INSERT INTO his_0306.power_record_all(tenant_id, business_type, code, calcu_property, start_time, end_time, param_name, param_value, `type`, create_time, old_param_value) "
    #        "VALUES('P3168', 1, '370882', 1,toDateTime('2024-08-26 00:00:00', 'Asia/Shanghai'), toDateTime('2024-08-27 00:00:00', 'Asia/Shanghai'), 'elec', '10', 1, toDateTime('2024-08-24 00:00:00', 'Asia/Shanghai'), '')")
    # sql = "SELECT param_value FROM his_0306.device_param_all WHERE tenant_id = 'P8950' AND device_id = 'HP00500032260530' AND param_name = 'INV.capacity' limit 1"
    # 调用实例方法
    sql1 = "SELECT param_value FROM his_0306.device_param_all WHERE tenant_id = 'P8950' AND device_id = 'HP00500032260530' AND param_name = 'INV.capacity' limit 1"
    sql ="SELECT param_value FROM his_0306.device_param_all WHERE receive_time = (SELECT IF(MAX(receive_time) = toDateTime('1970-01-01 00:00:00', 'Asia/Shanghai'), toStartOfDay(toTimeZone(now(), 'Asia/Shanghai')), MAX(receive_time)) ASmax_receive_time FROM his_0306.device_param_all WHERE receive_time >= toStartOfDay(toTimeZone(now(), 'Asia/Shanghai')) AND tenant_id = 'P8950' AND device_id = 'HP00500032260530')"
    data = data_preparation.data_select(sql1)
    # print(data)